package cn.aitrox.ry.app.web.controller.auth;

import cn.aitrox.ry.app.web.vo.auth.permission.in.AuthPermissionCreateInVo;
import cn.aitrox.ry.app.web.vo.auth.permission.in.AuthPermissionCreateOutVo;
import cn.aitrox.ry.app.web.vo.auth.permission.in.AuthPermissionSearchInVo;
import cn.aitrox.ry.app.web.vo.auth.permission.in.AuthPermissionUpdateInVo;
import cn.aitrox.ry.app.web.vo.auth.permission.out.AuthPermissionDetailOutVo;
import cn.aitrox.ry.app.web.vo.auth.resource.out.AuthResourceDetailOutVo;
import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.auth.dto.permission.in.AuthPermissionCreateInDto;
import cn.aitrox.ry.service.auth.dto.permission.in.AuthPermissionSearchInDto;
import cn.aitrox.ry.service.auth.dto.permission.in.AuthPermissionUpdateInDto;
import cn.aitrox.ry.service.auth.dto.permission.out.AuthPermissionDetailDto;
import cn.aitrox.ry.service.auth.feign.AuthPermissionFeign;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class AuthPermissionController {

    @Autowired
    @Lazy
    private AuthPermissionFeign authPermissionFeign;

    @PostMapping(value = "/auth/permission")
    RespResult<AuthPermissionCreateOutVo> create(@Validated @RequestBody AuthPermissionCreateInVo input) {
        RespResult<Integer> respResult = authPermissionFeign.create(new AuthPermissionCreateInDto(input.getName(), input.getResourceIds()));
        if (!respResult._isOk()) {
            return RespResult.secByError(respResult.getCode(), respResult.getMsg());
        }
        return RespResult.ok(new AuthPermissionCreateOutVo(respResult.getData()));
    }

    @PutMapping(value = "/auth/permission")
    RespResult update(@Validated @RequestBody AuthPermissionUpdateInVo input) {
        return authPermissionFeign.update(new AuthPermissionUpdateInDto(input.getId(), input.getName(), input.getResourceIds()));
    }

    @GetMapping(value = "/auth/permission{id}")
    RespResult<AuthPermissionDetailOutVo> read(@PathVariable("id") Integer id) {
        RespResult<AuthPermissionDetailDto> respResult = authPermissionFeign.read(id);
        if (!respResult._isOk()) {
            return RespResult.secByError(respResult.getCode(), respResult.getMsg());
        }

        AuthPermissionDetailDto permissionDetail = respResult.getData();

        List<AuthResourceDetailOutVo> resourceDetailOutVo = null;
        if (!ArrayUtil.isEmpty(permissionDetail.getResourceList())) {
            resourceDetailOutVo = permissionDetail.getResourceList().stream().map(x -> AuthResourceDetailOutVo.mapAuthResourceDetailOutVo(x)).collect(Collectors.toList());
        }

        return RespResult.ok(new AuthPermissionDetailOutVo(permissionDetail.getId(), permissionDetail.getName(), permissionDetail.getCreateTime(), permissionDetail.getUpdateTime(), resourceDetailOutVo));
    }

    @DeleteMapping(value = "/auth/permission/{id}")
    RespResult delete(@PathVariable("id") Integer id) {
        return authPermissionFeign.delete(id);
    }

    @PostMapping(value = "/auth/permission/search")
    RespResult<CommonPageInfo<AuthPermissionDetailDto>> search(@RequestBody AuthPermissionSearchInVo input) {
        CommonPageInfo<AuthPermissionDetailDto> pageInfo = authPermissionFeign.search(new AuthPermissionSearchInDto(input.getName()));
        if (!ArrayUtil.isEmpty(pageInfo.getList())) {
            return RespResult.ok(new CommonPageInfo(pageInfo.getTotal(), pageInfo.getPages(), null));
        }

        List<AuthPermissionDetailOutVo> list = pageInfo.getList().stream().map(x -> {
            List<AuthResourceDetailOutVo> resourceDetailOutVo = null;
            if (!ArrayUtil.isEmpty(x.getResourceList())) {
                resourceDetailOutVo = x.getResourceList().stream().map(y -> AuthResourceDetailOutVo.mapAuthResourceDetailOutVo(y)).collect(Collectors.toList());
            }
            return new AuthPermissionDetailOutVo(x.getId(), x.getName(), x.getCreateTime(), x.getUpdateTime(), resourceDetailOutVo);
        }).collect(Collectors.toList());

        return RespResult.ok(new CommonPageInfo(pageInfo.getTotal(), pageInfo.getPages(), list));
    }
}
